# coding: utf-8

'''
http://wiki.it.yzs.io:8090/pages/viewpage.action?pageId=40639911
'''

import sys
import time
from utils.req import *
from utils.jktest import *


def send_krc_post(url, params, jsondump=True, sign=""):
    header = {'Connection' : 'close'}
    r = None
    if jsondump:
        header = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close', 'signature':sign}
        r = requests.post(url, data=json.dumps(params), headers=header)
    else:
        r = requests.post(url, data=params, headers=header)
    ret = r.content
    # print '-' * 20
    # print url
    # print params
    # print '-' * 20
    # print '>>', ret
    return ret

def request_skill_krc(params,sign):
    url = "http://%s/unios-skill-krc/rest/v1/krc" % Server
    ret = send_krc_post(url, params, jsondump=True, sign=sign)
    print ret
    return ret

def perf_test():
    tid = '0'
    log = 'log.log'
    duration = 10

    if len(sys.argv) >= 3:
        tid = sys.argv[1]
        duration = int(sys.argv[2])
    if len(sys.argv) >= 4:
        log = sys.argv[3]

    pass_ = 0
    total = 0
    print "thread %s will run %s(s)" % (tid, duration)
    begin = time.time()
    while time.time() < begin + duration:
        ret = request_skill_krc()
        if '难过' in ret:
            pass_ += 1
        total += 1

    dur = (time.time() - begin)
    tps = pass_ / dur
    reps_time = dur / pass_
    result = '\nthread: %s: total: %d, pass_: %d, TPS: %.2f, reps_time: %.4f, elapsed_time: %.2f\n' %(tid, total, pass_, tps, reps_time, dur)
    print result.strip()
    with open(log, 'a') as log:
        log.write(result)


if __name__ == '__main__':
    Server = "172.18.10.66:8080" # UAT
    AppKey = "3dcddlnx7ddlb2xatjxtbtxha6xah7iogajzkqie"

    ########### Online #############
    # Server = "172.20.72.3:8080"
    # Server = "172.20.81.241:8080"
    Server = "172.20.7.248:8080"

    ################### 珠穆朗玛峰有多高 #################
    params = {"request":{"timstamp":1582783236374,"type":"end","intent":{"slots":[{"isConfirm":"1","isFocus":"0","isMust":"1","name":"entityName","sort":1,"value":"珠穆朗玛峰"},{"isConfirm":"1","isFocus":"0","isMust":"1","name":"property","sort":2,"value":"海拔"},{"isConfirm":"1","isFocus":"0","isMust":"1","name":"style","sort":3,"value":"CQA_guidance"},{"isConfirm":"1","isFocus":"0","isMust":"1","name":"category","sort":4,"value":"mountain"}],"isConfirm":"1","name":"unios.usk.unisound.kbqa.mountain.altitude"},"info":{"type":"TEXT","recongize":"珠穆朗玛峰有多高"}},"session":{"new":True,"attributes":[{"user":"珠穆朗玛峰有多高"},{}],"sessionId":"56d35bc8-827c-4ae6-90e7-356d290282ef"},"context":{"skill":{"skillId":"unios.usk.unisound.kbqa.mountain.river","service":"unios.usk.unisound.kbqa.mountain.river","skillVersion":""},"user":{},"device":{"interfaces":{"vedio":{"formats":[],"codecs":[]},"audio":{"formats":[],"codecs":[]}},"udid":"885b6751-773e-4f99-9f9b-d5d75d607b4b"}},"version":"V1.0"}
    sign = "30b3b359d754f37812d1c77e0fa9179b1657fb8d"

    ################## 用口是心非怎么造句 ##################
    params = {"request":{"timstamp":1582783650797,"type":"end","intent":{"slots":[{"isConfirm":"1","isFocus":"0","isMust":"1","name":"enableFuzzy","sort":1,"value":"true"},{"isConfirm":"1","isFocus":"0","isMust":"1","name":"entityName","sort":2,"value":"口是心非"},{"isConfirm":"1","isFocus":"0","isMust":"1","name":"answerFormat","sort":3,"value":"%s"},{"isConfirm":"1","isFocus":"0","isMust":"1","name":"property","sort":4,"value":"例句"},{"isConfirm":"1","isFocus":"0","isMust":"1","name":"category","sort":5,"value":"例句词语"}],"isConfirm":"1","name":"unios.usk.unisound.kbqa.make.sentence"},"info":{"type":"TEXT","recongize":"用口是心非怎么造句"}},"session":{"new":True,"attributes":[{"user":"用口是心非怎么造句"},{}],"sessionId":"65e8781a-6559-4177-b236-0230058145d6"},"context":{"skill":{"skillId":"unios.usk.unisound.kbqa.make.sentence","service":"unios.usk.unisound.kbqa.make.sentence","skillVersion":""},"user":{},"device":{"interfaces":{"vedio":{"formats":[],"codecs":[]},"audio":{"formats":[],"codecs":[]}},"udid":"885b6751-773e-4f99-9f9b-d5d75d607b4b"}},"version":"V1.0"}
    sign = "0fb14c89a0911e1c6557f032d6cc3e57702a1c6f"

    request_skill_krc(params, sign)

    # perf_test()
